//
//  FindPeopleController.m
//  CMJWeibo
//
//  Created by BH on 16/3/16.
//
//  搜索联系人或群组  https://api.weibo.com/2/search/suggestions/users.json

#import "FindPeopleController.h"
#import "SearchBar.h"
#import "UIView+MJ.h"
#import "AccountTool.h"
#import "HttpTool.h"
#import "NSString+URLEncode.h"
#import "FindPeopleModel.h"

@interface FindPeopleController ()
{
    NSArray *findlist;
}

@end

@implementation FindPeopleController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelSearch:)];
    
    SearchBar *searchB = [[SearchBar alloc] init];
    searchB.frame = CGRectMake(0, 0, self.view.frame.size.width-20, 32);
    [searchB addTarget:self action:@selector(textFieldEditing:) forControlEvents:UIControlEventEditingChanged];
    searchB.font = [UIFont systemFontOfSize:14];
    searchB.placeholder = @"搜索联系人和群";
    searchB.returnKeyType = UIReturnKeySearch;
    [searchB becomeFirstResponder];
    self.navigationItem.titleView = searchB;
}

/** 点击取消按钮-取消搜索 */
-(void)cancelSearch:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/** 开始输入时调用 */
-(void)textFieldEditing:(UITextField *)textField
{
    NSDictionary *param = @{@"access_token":[AccountTool account].access_token,
                            @"q":[textField.text URLEncodedString]};
    
    [HttpTool getWithURL:@"https://api.weibo.com/2/search/suggestions/users.json" params:param success:^(id json) {
        
        NSLog(@"%@",json);
        
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *dict in json) {
            FindPeopleModel *model = [[FindPeopleModel alloc] init];
            model.screen_name = dict[@"screen_name"];
            model.followers_count = [dict[@"followers_count"] integerValue];
            model.uid = dict[@"uid"];
            [array addObject:model];
        };
        findlist = [NSArray arrayWithArray:array];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return findlist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"findPeopleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    cell.textLabel.text = [findlist[indexPath.row] screen_name];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
